
#coding:utf-8
import re
from flask import request
from flask import session
from flask import render_template
import models.material as material
import json as sys_json
import libs.util.json_enable_chinese as json
import libs.util.leave_HMS_of_time as trans_time

def materials_route(app):
    @app.route('/note/material/new',methods=['GET','POST'])
    def new_material():
        if(request.method == 'GET'):
            return render_template('pub-notification/new-material.html',user = session, topic_id = -1, type = u"新增")
        else:
            #@TODO 要换掉
            title = request.form['title']
            content = request.form['content']
            stu_id = session['stu_id']
            name = session['name']
            #重要！
            content = re.sub('_src[^>]+[baidu]+[^>]+gif"?', ' ', content)
            args = {
                "stu_id":stu_id,
                 "name":name,
                 "title":title,
                "content":content,
                "edit_time_period":0
            }
            return  sys_json.dumps(material.new_material(args))

    @app.route('/note/material/edit', methods=['GET', 'POST'])
    def edit_material():
        topic_id = request.args.get("id")
        if(request.method == "GET"):
             return render_template('pub-notification/new-material.html',topic_id = topic_id, type = u"编辑",user = session)
        else:
             #@TODO 要换掉
            title = request.form['title']
            content = request.form['content']
            stu_id = session['stu_id']
            name = session['name']
            #重要！
            content = re.sub('_src[^>]+[baidu]+[^>]+gif"?', ' ', content)
            args = {
                "stu_id":stu_id,
                 "name":name,
                 "title":title,
                "content":content,
                "edit_time_period":0,
                "topic_id":topic_id
            }
            return  sys_json.dumps(material.new_material(args))

    @app.route('/note/material/get-json', methods=["GET"])
    def get_json():
        '根据素材的主题id获取内容(json)'
        material_topic_id = request.args.get('id')
        material_id = material.get_latest_material(material_topic_id)
        content = material.get_content_material_by_id(material_id)
        return json.json_aesop(content).json_encode()

    @app.route('/note/material/get-html', methods=['GET'])
    def get_html():
        material_topic_id = request.args.get('id')
        material_id = material.get_latest_material(material_topic_id)
        content = material.get_content_material_by_id(material_id)
        return render_template('notification.html',title=content["title"]
                               , author = content["author"], content = content["content"]
                               , date = trans_time.leave_hms(content["date"]))


    @app.route('/note/material/preview', methods=['GET'])
    def show_html():
        '根据素材的id获取内容'
        material_id = request.args.get('id')
        content = material.get_content_material_by_id(int(material_id))
        return render_template('preview.html',title=content["title"]
                               , author = content["author"], content = content["content"]
                               , date = trans_time.leave_hms(content["date"]))

    @app.route('/note/material/list', methods=['GET'])
    def list_material():
        '根据stuid获取其所有素材'
        #@TODO
        stu_id = session['stu_id'] = '121270026'
        page = request.args.get('page')
        if(page):
            page = int(request.args.get('page'))
        try:
            material_id_list = material.get_materials_by_stu_id(stu_id, page-1)
        except:
            material_id_list = material.get_materials_by_stu_id(stu_id)

        materials_list = [ material.get_content_material_by_id(material.get_latest_material(x)) for x in material_id_list]
        return render_template('pub-notification/material-list.html',material = materials_list,user = session)
